package com.tensquare.sms;

import com.aliyuncs.exceptions.ClientException;
import com.tensquare.sms.util.SmsUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @ClassName SmsListener
 * @Description 短信监听类
 * @Author zhangshuai
 * @Date 2019/1/20 16:49
 * @Version 1.0
 **/

@Component
@RabbitListener(queues = "sms")
public class SmsListener {

    @Autowired
    private SmsUtil smsUtil;

    @Autowired
    private Environment env;

   /* @Value("${aliyun.sms.sign_name}")
    private String signName;//签名

    @Value("${aliyun.sms.template_code}")
    private String templateCode;//模板编号*/


    @RabbitHandler
    public void sendSms(Map<String, String> map) {

        String singName = env.getProperty("aliyun.sms.sign_name");
        String tempalteCode = env.getProperty("aliyun.sms.template_code");

        System.out.println(singName + "===========" + tempalteCode);

        try {
            System.out.println("手机号码:" + map.get("code"));
            smsUtil.sendSms(map.get("mobile"), tempalteCode, singName, "{\"code\":" + map.get("code") + "}");
        } catch (ClientException e) {
            System.out.println(e.getMessage());
        }
    }


}
